package com.roads.engineering.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.roads.common.core.domain.entity.SysUser;
import com.roads.common.core.text.Convert;
import com.roads.common.utils.SecurityUtils;
import com.roads.engineering.domain.ChangeOrderMaster;
import com.roads.engineering.mapper.ChangeOrderMasterMapper;
import com.roads.project.domain.ProBoqMeterage;
import com.roads.project.domain.ProInstitution;
import com.roads.project.mapper.ProBoqMeterageMapper;
import com.roads.project.mapper.ProInstitutionMapper;
import com.roads.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.roads.engineering.mapper.ChangeOrderDetailMapper;
import com.roads.engineering.domain.ChangeOrderDetail;
import com.roads.engineering.service.IChangeOrderDetailService;

/**
 * 变更令申报详情Service业务层处理
 * 
 * @author nhp
 * @date 2022-01-12
 */
@Service
public class ChangeOrderDetailServiceImpl implements IChangeOrderDetailService {

    @Autowired
    private ChangeOrderDetailMapper mapper;

    @Autowired
    private ProBoqMeterageMapper meterageMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 变更令申报详情主键
     * @return 变更令申报详情
     */
    @Override
    public ChangeOrderDetail getModelById(Long id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询列表
     * 
     * @param model 变更令申报详情
     * @return 变更令申报详情
     */
    @Override
    public List<ChangeOrderDetail> selectModelList(ChangeOrderDetail model) {
        return mapper.selectModelList(model);
    }

    /**
     * 查询未选中的计量清单
     *
     * @param model 变更令详情
     * @return 计量清单集合
     */
    @Override
    public List<ProBoqMeterage> unallocatedMeterageList(ChangeOrderDetail model) {
        return mapper.unallocatedMeterageList(model);
    }

    /**
     * 新增
     * 
     * @param model 变更令申报详情
     * @return 结果
     */
    @Override
    public int insert(ChangeOrderDetail model) {
        return mapper.insert(model);
    }

    /**
     * 计量清单选择保存
     *
     * @param changeId 变更令主表ID
     * @param ids 计量清单ID集合
     * @return 结果
     */
    @Override
    public int save(Long changeId, String ids) {
        List<ProBoqMeterage> meterageList = meterageMapper.selectModelListByIds(Convert.toStrArray(ids));
        List<ChangeOrderDetail> details = new ArrayList<ChangeOrderDetail>();
        // 当前登录用户
        SysUser loginUser = userMapper.selectUserById(SecurityUtils.getUserId());
        for (ProBoqMeterage meterage : meterageList) {
            ChangeOrderDetail detail = new ChangeOrderDetail();
            detail.setChangeId(changeId);
            detail.setMeterageBoqCode(meterage.getMeterageBoqCode());
            detail.setBoqCode(meterage.getBoqCode());
            detail.setBoqName(meterage.getBoqName());
            detail.setAncestors(meterage.getAncestors());
            detail.setOrderNum(meterage.getOrderNum());
            detail.setUnit(meterage.getUnit());
            detail.setPrice(meterage.getPrice());
            detail.setAmount(null);
            detail.setTotal(null);
            detail.setPlace(meterage.getPlace());
            detail.setItem(meterage.getItem());
            detail.setBeginStation(meterage.getBeginStation());
            detail.setEndStation(meterage.getEndStation());
            detail.setDept(loginUser.getDeptId());
            detail.setDeptName(loginUser.getDept().getDeptName());
            detail.setUser(loginUser.getUserName());
            detail.setUserName(loginUser.getNickName());
            details.add(detail);
        }
        // 批量插入
        mapper.insertBatch(details);
        return 1;
    }

    /**
     * 修改
     * 
     * @param model 变更令申报详情
     * @return 结果
     */
    @Override
    public int update(ChangeOrderDetail model) {
        return mapper.update(model);
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] ids) {
        return mapper.deleteByIds(ids);
    }

    /**
     * 删除
     * 
     * @param id 变更令申报详情主键
     * @return 结果
     */
    @Override
    public int delete(Long id) {
        return mapper.delete(id);
    }
}
